﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TD.Interface.Credits
{
    internal class Word : DrawableGameComponent
    {
        private const int SPACING = 0x08;

        public Effect Effect
        {
            set
            {
                foreach (Character character in characters)
                {
                    character.Effects.Add(value);
                }
            }
        }
        private List<Character> characters = new List<Character>();
        private string word;

        public Point Position { get; set; }

        public Word(Game game, string word) :
            base(game)
        {
            this.word = word;
            this.Position = Point.Zero;
        }

        public override void Initialize()
        {
            //Effects = new List<Effect>();
            
            for (int i = 0; i < word.Length; i++)
            {
                Character character = new Character(Game, word[i], BlendState.Additive);
                characters.Add(character);
                Game.Components.Add(character);
            }
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            int offset = Position.X;
            for (int i = 0; i < word.Length; i++)
            {
                Character character = characters[i];
                character.Position = new Vector2(offset, Position.Y);
                offset += (character.Width + SPACING);
            }
        }

        public override void Update(GameTime gameTime)
        {
            foreach (Character character in characters)
            {
                //character.Update(gameTime);
                //Debug.WriteLine("Manual Update");
            }

            base.Update(gameTime);
        }

        protected override void OnEnabledChanged(object sender, EventArgs args)
        {
            if (CharactersAvailable())
            {
                foreach (Character character in characters)
                {
                    character.Enabled = this.Enabled;
                }
            }

            base.OnEnabledChanged(sender, args);
        }

        protected override void OnVisibleChanged(object sender, EventArgs args)
        {
            if (CharactersAvailable())
            {
                foreach (Character character in characters)
                {
                    character.Visible = this.Visible;
                }
            }
            
            base.OnVisibleChanged(sender, args);
        }

        private bool CharactersAvailable()
        {
            if (characters != null)
            {
                if (characters.Count > 0)
                    return true;
            }

            return false;
        }

        public void Reset()
        {
            foreach (Character character in characters)
                character.Reset();
        }
    }
}
